/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compilador.analisadorLexico.automatos;

import compilador.analisadorLexico.Alfabeto;
import compilador.analisadorLexico.CodeReader;
import compilador.analisadorLexico.Token;
import compilador.analisadorLexico.TokenClass;
import compilador.analisadorLexico.TokenErro;

/**
 *
 * @author Aluno
 */
public class AutomatoOperador implements IAutomato{
    
    private CodeReader reader;

    public AutomatoOperador(CodeReader reader){
        this.reader = reader;
    }
    
    public Token extraiToken() {
		
        // TODO Auto-generated method stub
        reader.clear();

        char caracter = reader.next();

        int linha = reader.getCurrentLine();

        Token token = null;

        if(Alfabeto.ehOperador(caracter)){
            boolean houveErro = false;
            char caracterNext = reader.next();
            
            switch(caracter){
                case '+':
                    if(caracterNext == '+')
                        reader.next();
                    break;
                
                case '-':
                    if(caracterNext == '-')
                        reader.next();
                    break;
                
                case '!':
                    if(caracterNext == '=')
                        reader.next();
                   break;
                    
                case '|':
                        if(caracterNext == '|')
                        reader.next();
                    else{
                        houveErro = true;
                    }
                    break;

                case '&':
                        if(caracterNext == '&')
                        reader.next();
                    else{
                        houveErro = true;
                    }
                    break;
                    
                case '<':
                    if(caracterNext == '=')
                        reader.next();
                    break;
                
                case '>':
                    if(caracterNext == '=')
                        reader.next();
                    break;
                    
                case '=':
                    if(caracterNext == '=')
                        reader.next();
                    break;
            }

            if(houveErro == false){
                String lexema = reader.closeToken();
                token = new Token(TokenClass.OPERADOR, lexema, linha);
                
            }else{
                String lexema = reader.closeToken();
                token = new TokenErro(lexema, linha, "Operador mal-formado");
                
            }
        }

        return token;
                
    }
    
}
    
